<template>
  <div>
    <a-form-model ref="wf_timelimitForm" :rules="rules" :model="wf_timelimit">
      <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="办结时限" has-feedback>
        <a-switch v-model="s_limitable" checked-children="开" un-checked-children="关" default-checked />
      </a-form-model-item>

      <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="日期类型" prop="daytype" has-feedback>
        <a-radio-group v-model="wf_timelimit.daytype" name="radioGroup" :default-value="1">
          <!--  -->
          <a-radio v-for="item in Tp_day" :key="item.value" :value="Number(item.value)">{{ item.label }}</a-radio>
        </a-radio-group>
      </a-form-model-item>
      <!-- 限时天数 -->
      <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="限时天数" prop="daylimit" has-feedback>
        <a-input-number style="width: 50%" v-model="wf_timelimit.daylimit" placeholder="请输入限时天数" :min="0" />
      </a-form-model-item>
      <!-- 告警天数 -->
      <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="告警天数" prop="daywarn" has-feedback>
        <a-input-number style="width: 50%" v-model="wf_timelimit.daywarn" placeholder="请输入告警天数" :max="0" />
      </a-form-model-item>

      <a-form-model-item>
        <div style="text-align: center">
          <a-button :style="{ marginRight: '8px' }" icon="close" @click="onClose"> 关闭 </a-button>
          <a-button type="primary" @click="onSubmit" icon="save" :loading="loading"> 保存 </a-button>
        </div>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
import { viewAct_dsn_form_define, limitSave } from '@/api/workflow/act_dsn_form_define.js'

export default {
  data() {
    return {
      s_limitable: false, // 办结时限开关
      Tp_day: this.$store.state.form.formData.dynamicDataList['Tp_day'] || [],
      labelCol: { span: 6 }, // label宽
      wrapperCol: { span: 18 }, // 组件宽
      loading: false, //按钮显示
      wf_timelimit: {
        daytype: null,
        daylimit: null,
        daywarn: null,
      }, //流程实例信息
      rules: {
        daytype: [
          {
            required: true,
            message: '请选择日期类型',
            trigger: 'blur',
          },
        ],
        daylimit: [
          {
            required: true,
            message: '请输入限时天数',
            trigger: 'blur',
          },
        ],
        daywarn: [
          {
            required: true,
            message: '请输入告警天数',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {},
  methods: {
    //  保存
    onSubmit() {
      // 开了办结时限校验其它项, 没开不检验
      this.wf_timelimit.limitable = this.s_limitable ? 3200 : 3201
      if (this.s_limitable) {
        this.$refs.wf_timelimitForm.validate((valid) => {
          if (valid) {
            // 告警天数不能大于限时天数
            if (-this.wf_timelimit.daywarn > this.wf_timelimit.daylimit) {
              this.$message.warning('告警天数不能大于限时天数')
              return
            }
            this.loading = true
            limitSave(this.wf_timelimit)
              .then((res) => {
                if (res.code == 0) {
                  this.$message.success('保存成功')
                  setTimeout(() => {
                    this.onClose()
                  }, 1000)
                }
              })
              .finally(() => {
                this.loading = false
              })
          }
        })
      } else {
        this.loading = true
        limitSave(this.wf_timelimit)
          .then((res) => {
            if (res.code == 0) {
              this.$message.success('保存成功')
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
    },
    // 初始化办结时限数据
    timelimitInit(formid) {
      console.log('formid', formid)
      viewAct_dsn_form_define({ formid: formid }).then((res) => {
        // console.log('表单定义', res)
        if (res.code == 0) {
          res.data.limitable == 3200 ? this.s_limitable = true : this.s_limitable = false
          this.wf_timelimit = res.data
        }
      })
    },
    // 关闭
    onClose() {
      this.$emit('close')
    },
  },
}
</script>

<style lang="scss" scoped>
</style>